package org.tony.web.spider;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Iterator;

import junit.framework.TestCase;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import org.tony.spider.common.GetRequestor;

public class DemoParse extends TestCase
{

  public void testParseBaiduTieba()
  {
    GetRequestor getReq = new GetRequestor();
    String content = getReq.startGet("http://tieba.baidu.com/p/1743691457");

    Document doc = Jsoup.parse(content);
    Elements eles = doc.select("p.d_post_content");

    for (int i = 0; i < eles.size(); i++)
    {
      Element temp = eles.get(i);
      if (temp.text().length() < 500 && i == 0)
      {
        continue;
      }
      else if (temp.text().length() < 500 && i > 0)
      {
        break;
      }
      printElement(temp);
    }
  }

  public void testParseTime()
  {
    GetRequestor getReq = new GetRequestor();
    String content = getReq.startGet("http://tieba.baidu.com/p/1743691457");

    Document doc = Jsoup.parse(content);
    System.out.println(content);
    System.out.println(doc.select("div.p_tail").first());
  }

  private void printElement(Element ele)
  {
    Iterator<Node> node = ele.childNodes().iterator();
    while (node.hasNext())
    {
      Node temp = node.next();
      if (temp instanceof TextNode)
      {
        System.out.println(((TextNode)temp).text());
      }
    }
  }

  public void testGetBookDir()
  {
    //http://book.zhulang.com/166128/
    GetRequestor getReq = new GetRequestor();
    String content = getReq.startGet("http://book.zhulang.com/166128/");
    Document doc = Jsoup.parse(content);
    Elements eles = doc.select("div.bookdetail").select("a[href]");
    for (Iterator<Element> eleIter = eles.iterator(); eleIter.hasNext();)
    {
      Element ele = eleIter.next();
      if (ele.childNodes().size() > 0)
      {
        File saveDir = new File("d:\\noval\\shengyu");
        if (!saveDir.exists())
        {
          saveDir.mkdirs();
        }
        Node textNode = ele.childNode(0);
        String url = ele.attr("href");
        String text = textNode.attr("text");
        if (text.matches("\u7B2C.+\u7AE0.+") && !url.contains("vip"))
        {
          File chaperFile = new File(saveDir, text + ".txt");
          if (!chaperFile.exists())
          {
            try
            {
              chaperFile.createNewFile();
              getDetailAndStorage(url, chaperFile);
            }
            catch (Exception e)
            {
              System.err.println(e);
            }

          }
        }
      }
    }
  }

  public void getDetailAndStorage(String url, File file) throws Exception
  {
    GetRequestor getReq = new GetRequestor();
    String content = getReq.startGet(url);
    Document doc = Jsoup.parse(content);
    Elements eles = doc.select("div.readpage_leftntxt");
    if (eles.size() == 1)
    {
      Element el = eles.first();
      el.select("p").remove();
      el.select("div").remove();
      Iterator<Node> nodes = el.childNodes().iterator();
      OutputStream fos = new FileOutputStream(file);
      while (nodes.hasNext())
      {
        Node node = nodes.next();
        if (node instanceof TextNode)
        {
          fos.write(((TextNode)node).text().getBytes("UTF-8"));
          fos.write("\n".getBytes("UTF-8"));
        }
      }
      fos.flush();
      fos.close();
    }
  }

  public void testMatch()
  {
    System.out.println("第八十八".matches("\u7B2C.+\u7AE0"));
  }

  public void testWriterFile() throws Exception
  {
    String fileName = "d:\\noval\\Chapter 1.txt";
    File file = new File(fileName);
    if (!file.exists())
    {
      file.getParentFile().mkdirs();
      file.createNewFile();
    }
    OutputStream fos = new FileOutputStream(file);
    fos.write("测试中文".getBytes("UTF-8"));
    fos.flush();
    fos.close();
  }

  public void testGet()
  {
    String url = "http://www.iciba.com/flux";
    GetRequestor gr = new GetRequestor();
    String content = gr.startGet(url);
    Document doc = Jsoup.parse(content);
    System.out.println(doc.select("div.dict_content"));
  }

}
